package net.qhzw.vip.qiyu.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.SysVideos;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysVideosService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("sys/videos")
@Api(tags = "帮助视频")
@Slf4j
public class SysVideosController {

    @Autowired
    private SysVideosService sysVideosService;

    @ApiOperation(value = "分页查询帮助视频")
    @GetMapping
    public JsonResult findByPage(Page<SysVideos> page, String title, Byte status) {
        Page<SysVideos> sysVideosPage = sysVideosService.findByPage(page, title, status);
        return JsonResult.ok(sysVideosPage);
    }

    @ApiOperation(value = "根据ID查询帮助视频")
    @GetMapping("{id}")
    public JsonResult getSysVideos(@PathVariable Long id) {
        SysVideos sysVideos = sysVideosService.getSysVideos(id);
        return JsonResult.ok(sysVideos);
    }


    @ApiOperation(value = "新增帮助视频")
    @PostMapping
    public JsonResult addSysVideos(@RequestBody SysVideos sysVideos) {
        log.info("新增帮助视频:{}", JSON.toJSONString(sysVideos));
        SysVideos sysVideosBD = getSysVideosDB(sysVideos);
        boolean save = sysVideosService.save(sysVideosBD);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改帮助视频")
    @PutMapping
    public JsonResult modifySysVideos(@RequestBody SysVideos sysVideos) {
        SysVideos sysVideosBD = getSysVideosDB(sysVideos);
        boolean update = sysVideosService.updateById(sysVideosBD);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改帮助视频状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody SysVideos sysVideos) {
        boolean update = sysVideosService.updateById(sysVideos);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "根据ID删除帮助视频")
    @DeleteMapping("{id}")
    public JsonResult delSysVideos(@PathVariable Long id) {
        boolean remove = sysVideosService.removeById(id);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    private SysVideos getSysVideosDB(SysVideos sysVideos) {
        SysVideos sysVideosBD = new SysVideos();
        BeanUtils.copyProperties(sysVideos, sysVideosBD);
        if (sysVideos.getId() == null) {
            sysVideosBD.setId(IdWorker.getId());
            sysVideosBD.setCreateBy(SecurityUtils.getUserId());
            sysVideosBD.setCreated(new Date());
            sysVideosBD.setStatus((byte) 1);
            return sysVideosBD;
        }
        return sysVideosBD;
    }


}
